using ImageCSharp.Core.Interfaces;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using System.ComponentModel.Composition;

namespace ImageCSharp.Plugins.Filters
{
    /// <summary>
    /// 高斯模糊滤镜
    /// </summary>
    [Export(typeof(IImagePlugin))]
    [Export(typeof(IFilterPlugin))]
    public class GaussianBlurFilter : IFilterPlugin
    {
        public string Name => "高斯模糊";
        public string Description => "应用高斯模糊效果";
        public string Category => "模糊";

        public Dictionary<string, object> Parameters { get; set; } = new()
        {
            { "Radius", 5.0f }
        };

        public Image ProcessImage(Image image)
        {
            var radius = Parameters.ContainsKey("Radius") ? (float)Parameters["Radius"] : 5.0f;

            // 创建新图像以避免修改原始图像
            var result = image.CloneAs<Rgba32>();
            result.Mutate(x => x.GaussianBlur(radius));
            return result;
        }
    }

    /// <summary>
    /// 锐化滤镜
    /// </summary>
    [Export(typeof(IImagePlugin))]
    [Export(typeof(IFilterPlugin))]
    public class SharpenFilter : IFilterPlugin
    {
        public string Name => "锐化";
        public string Description => "增强图像细节和边缘";
        public string Category => "锐化";

        public Dictionary<string, object> Parameters { get; set; } = new()
        {
            { "Amount", 1.0f }
        };

        public Image ProcessImage(Image image)
        {
            var amount = Parameters.ContainsKey("Amount") ? (float)Parameters["Amount"] : 1.0f;
            
            image.Mutate(x => x.GaussianSharpen(amount));
            return image;
        }
    }

    /// <summary>
    /// 边缘检测滤镜
    /// </summary>
    [Export(typeof(IImagePlugin))]
    [Export(typeof(IFilterPlugin))]
    public class EdgeDetectionFilter : IFilterPlugin
    {
        public string Name => "边缘检测";
        public string Description => "检测图像中的边缘";
        public string Category => "边缘检测";

        public Dictionary<string, object> Parameters { get; set; } = new()
        {
            { "Threshold", 0.5f }
        };

        public Image ProcessImage(Image image)
        {
            image.Mutate(x => x.DetectEdges());
            return image;
        }
    }

    /// <summary>
    /// 对比度调整滤镜
    /// </summary>
    [Export(typeof(IImagePlugin))]
    [Export(typeof(IFilterPlugin))]
    public class ContrastFilter : IFilterPlugin
    {
        public string Name => "对比度";
        public string Description => "调整图像对比度";
        public string Category => "调整";

        public Dictionary<string, object> Parameters { get; set; } = new()
        {
            { "Amount", 1.0f }
        };

        public Image ProcessImage(Image image)
        {
            var amount = Parameters.ContainsKey("Amount") ? (float)Parameters["Amount"] : 1.0f;
            
            image.Mutate(x => x.Contrast(amount));
            return image;
        }
    }
}